﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UlsStudio.Engine.Database;

namespace UlsStudio.Engine
{
    public class DatabaseLoadedUlsLogEntry : AbstractUlsLogEntry
    {
        /// <summary>
        /// ID field in DB
        /// </summary>
        public int DatabaseID { get; set; }

        /// <summary>
        /// Set by grid. Move this property.
        /// </summary>
        public int ViewIndex { get; set; }


        /// <summary>
        /// Populate record from a datareader
        /// </summary>
        internal static DatabaseLoadedUlsLogEntry LoadFromReader(DataReaderCache r)
        {
            DateTime ts = DateTime.MinValue;
            Guid g = Guid.Empty;
            string strGuid = string.Empty;

            if (r.ExistsField(Constants.FIELD_NAME_TIMESTAMP)) ts = r.Reader.GetDateTime(r.GetOrdinal(Constants.FIELD_NAME_TIMESTAMP));
            if (r.ExistsField(Constants.FIELD_NAME_CORRELATION)) strGuid = r.GetString(Constants.FIELD_NAME_CORRELATION);
            if (!string.IsNullOrEmpty(strGuid))
            {
                try
                {
                    g = new Guid(strGuid);
                }
                catch (FormatException)
                {
                    // Ignore
                }

            }

            //Logging.Writeline("Load item: rowid={0}", r.GetInt32(r.GetOrdinal(Constants.FIELD_NAME_ROWID)));
            DatabaseLoadedUlsLogEntry l = new DatabaseLoadedUlsLogEntry();
            if (r.ExistsField(Constants.FIELD_NAME_MSG)) l.Message = r.Reader.GetString(r.GetOrdinal(Constants.FIELD_NAME_MSG));
            if (r.ExistsField(Constants.FIELD_NAME_ID)) l.DatabaseID = r.Reader.GetInt32(r.GetOrdinal(Constants.FIELD_NAME_ID));
            if (r.ExistsField(Constants.FIELD_NAME_TIMESTAMP)) l.DateTime = r.Reader.GetDateTime(r.GetOrdinal(Constants.FIELD_NAME_TIMESTAMP));
            if (r.ExistsField(Constants.FIELD_NAME_PROCESS)) l.Process = r.GetString(Constants.FIELD_NAME_PROCESS);
            if (r.ExistsField(Constants.FIELD_NAME_TID)) l.ThreadID = r.GetString(Constants.FIELD_NAME_TID);

            if (r.ExistsField(Constants.FIELD_NAME_PRODUCT)) l.Area = r.GetString(Constants.FIELD_NAME_PRODUCT);
            if (r.ExistsField(Constants.FIELD_NAME_CATEGORY)) l.Category = r.GetString(Constants.FIELD_NAME_CATEGORY);
            if (r.ExistsField(Constants.FIELD_NAME_EVENTID)) l.EventID = r.GetString(Constants.FIELD_NAME_EVENTID);
            if (r.ExistsField(Constants.FIELD_NAME_LEVEL)) l.Level = r.GetString(Constants.FIELD_NAME_LEVEL);
            l.Correlation = g;
            if (r.ExistsField(Constants.FIELD_NAME_MSG)) l.Message = r.GetString(Constants.FIELD_NAME_MSG);

            if (r.ExistsField(Constants.FIELD_NAME_SOURCE)) l.Source = r.GetString(Constants.FIELD_NAME_SOURCE);
            //if (r.ExistsField(Constants.FIELD_NAME_ROWID)) l.RowID = r.Reader.GetInt32(r.GetOrdinal(Constants.FIELD_NAME_ROWID));         // Same as ID, basically

            return l;
        }
    }
}
